import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private JTextField accountField;
    private JPasswordField passwordField;

    public LoginFrame() {
        setTitle("MBTI职业性格测试 - 用户登录");
        setSize(400, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 2));

        JLabel accountLabel = new JLabel("账号:");
        panel.add(accountLabel);

        accountField = new JTextField();
        panel.add(accountField);

        JLabel passwordLabel = new JLabel("密码:");
        panel.add(passwordLabel);

        passwordField = new JPasswordField();
        panel.add(passwordField);

        JButton loginButton = new JButton("登录");
        panel.add(loginButton);

        JButton registerButton = new JButton("注册");
        panel.add(registerButton);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = accountField.getText();
                String password = new String(passwordField.getPassword());
                UserDAO userDAO = new UserDAOImpl();
                User user = userDAO.findByAccountAndPIN(account, password);
                if (user!= null) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "登录成功！");
                    // 根据用户类型打开相应界面（这里假设只有测试用户类型，可根据实际扩展）
                    new TestUserFrame(user.getAccount()).setVisible(true);
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "账号或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegisterFrame().setVisible(true);
            }
        });

        add(panel);
    }
}